package Parser;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

import net.sf.samtools.SAMFileHeader;
import net.sf.samtools.SAMFileReader;

import org.broad.igv.bbfile.BBFileReader;

public class CheckReference {

	private File file;
	private ArrayList<String> list = new ArrayList<>();

	public CheckReference(File file) {
		
		this.file = file;
		String extension = this.file.getName().substring(this.file.getName().lastIndexOf(".") + 1).toUpperCase();
		if (extension.contains("GFF")) {
			this.list = getRefOfBedGffGtfBedgraph(file, this.list);
		} else if (extension.contains("BEDGRAPH")) {
			this.list = getRefOfBedGffGtfBedgraph(file, this.list);
		} else if (extension.contains("WIG")) {
			//TODO:
		} else if (extension.contains("BED")) {
			this.list = getRefOfBedGffGtfBedgraph(file, this.list);
		} else if (extension.contains("BB")) {
			this.list = getRefOfBigBedWig(file, this.list);
		} else if (extension.contains("SAM")) {
			this.list = getRefOfBam(file, this.list);
		} else if (extension.contains("BAM")) {
			this.list = getRefOfBam(file, this.list);
		} else if (extension.contains("GTF")) {
			this.list = getRefOfBedGffGtfBedgraph(file, this.list);
		}
		else if (extension.contains("FASTA")) {
			this.list = getRefOfFasta(file, this.list);
		}
		else if (extension.contains("FASTQ")) {
			//TODO:
		}
		else if (extension.contains("BW")) {
			this.list = getRefOfBigBedWig(file, this.list);
		}
		else if (extension.contains("GBK")) {
			this.list = getRefOfGenBank(file, this.list);
		}
		else if (extension.contains("EMBL")) {
			this.list = getRefOfEmbl(file, this.list);
		}
	}

	
	private ArrayList<String> getRefOfFasta(File file2, ArrayList<String> list2) {
		try {
			BufferedReader bbf = new BufferedReader(new FileReader(file2));
			String line = "";
			while((line = bbf.readLine()) != null){
				if(line.startsWith(">")){
					if(!list2.contains(line.substring(1))){
						list2.add(line.substring(1));
					}
				}
			}
			bbf.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list2;
	}
	
	private ArrayList<String> getRefOfEmbl(File file2, ArrayList<String> list2) {
		try {
			BufferedReader bbf = new BufferedReader(new FileReader(file2));
			String line = "";
			while ((line = bbf.readLine()) != null) {
				if (line.startsWith("ID")) {
					StringTokenizer st = new StringTokenizer(line);
					st.nextToken();
					list2.add(st.nextToken().toString());
				}
			}
			bbf.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list2;
	}
	
	private ArrayList<String> getRefOfGenBank(File file2, ArrayList<String> list2) {
		try {
			BufferedReader bbf = new BufferedReader(new FileReader(file2));
			String line = "";
			while ((line = bbf.readLine()) != null) {
				if (line.startsWith("LOCUS")) {
					StringTokenizer st = new StringTokenizer(line);
					st.nextToken();
					list2.add(st.nextToken().toString());
				}
			}
			bbf.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list2;
	}


	private ArrayList<String> getRefOfBigBedWig(File file2, ArrayList<String> list2) {
		BBFileReader bbf;
		try {
			bbf = new BBFileReader(file2.getAbsolutePath());
			list2 = bbf.getChromosomeNames();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list2;
	}



	private ArrayList<String> getRefOfBedGffGtfBedgraph(File file, ArrayList<String> list){
		BufferedReader br;
		try {
			br = new BufferedReader(new FileReader(file));
			String line = "";
			while((line = br.readLine()) != null){
				if(!list.contains(line.split("\t")[0]))
					list.add(line.split("\t")[0]);
			}
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	
	private ArrayList<String> getRefOfBam(File file, ArrayList<String> list) {
		@SuppressWarnings("resource")
		SAMFileHeader header = new SAMFileReader(file).getFileHeader();
		String[] reflist = header.getTextHeader().split("\t");
		for (int i = 0; i < reflist.length; i++) {
			if(reflist[i].contains("SN:")){
				list.add(reflist[i].replace("SN:", ""));
			}
		}
		return list;
	}



	public ArrayList<String> getList() {
		return list;
	}



	public void setList(ArrayList<String> list) {
		this.list = list;
	}
}
